package com.example.emergency;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.test.suitebuilder.TestMethod;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class RetrieveData extends ListActivity{
	
	static SQLiteDatabase db=null;
	private TempDB mDbtemp;
	 ArrayList<String> mArrayList = new ArrayList<String>();
	 private static final int ACTIVITY_CREATE = 0;
	    private static final int ACTIVITY_EDIT = 1;
	    private static final int DELETE_ID = Menu.FIRST + 1;
	    private Cursor cursor;
	
	 


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.notes_list);
        this.getListView().setDividerHeight(2);
        mDbtemp = new TempDB(this);
        mDbtemp.open();
       fillData();
      registerForContextMenu(getListView());
	    
	}
	  // Create the menu based on the XML defintion
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.layout.listmenu, menu);
        return true;
    }

    // Reaction to the menu selection
  @Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {

        switch (item.getItemId()) {
        case R.id.button3:
            createTodo();
            return true;
        }
        return super.onMenuItemSelected(featureId, item);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case R.id.button3:
            createTodo();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    //@Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
        case DELETE_ID:
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
            mDbtemp.deleteNote(info.id);
            fillData();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    private void createTodo() {

        Intent i = new Intent(this, Empty.class);
        startActivityForResult(i, ACTIVITY_CREATE);
    }

    // ListView and view (row) on which was clicked, position and
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {

        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, Empty.class);
        i.putExtra(TempDB.KEY_ID, id);
        // Activity returns an result if called with startActivityForResult

        startActivityForResult(i, ACTIVITY_EDIT);
    }

    // Called with the result of the other activity
    // requestCode was the origin request code send to the activity
    // resultCode is the return code, 0 is everything is ok
    // intend can be use to get some data from the caller
   
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        super.onActivityResult(requestCode, resultCode, intent);
        fillData();

    }

    private void fillData() {

        cursor = mDbtemp.fetchAllNotes();
        startManagingCursor(cursor);

        String[] from = new String[] {
        		TempDB.KEY_KEY
        };
        int[] to = new int[] {
            R.id.label
        };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row, cursor, from, to);
        setListAdapter(notes);
    }
    

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);
        //menu.add(0, DELETE_ID, 0, R.string.menu_delete);
    }

    @Override
    protected void onDestroy() {

        super.onDestroy();
        if (mDbtemp != null) {
        	mDbtemp.close();
            finish();
        }
    }
	

}
